package com.batsoft.trade.api;

import com.gomyb.redis.enable.EnableRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableRedis
@EnableFeignClients(basePackages = {"batsoft.user"})
@ComponentScan(basePackages = {"com.batsoft"})
public class BatsoftApiApplication {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private DiscoveryClient discoveryClient;

    public static void main(String[] args) {
        SpringApplication.run(BatsoftApiApplication.class, args);
    }

    /**
     * 获取所有服务
     */
    @RequestMapping("/services/{name}")
    public Object services(@PathVariable("name") String name) {
        return discoveryClient.getInstances(name);
    }

    /**
     * 轮询某一个服务
     */
    @RequestMapping("/discover/{name}")
    public Object discover(@PathVariable("name") String name) {
        return loadBalancerClient.choose(name).getUri().toString();
    }

}
